package com.test.demo.ext


import android.text.TextUtils
import android.widget.ImageView
import com.apkfuns.logutils.LogUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.test.demo.R
import java.io.BufferedReader
import java.io.FileReader
import java.io.IOException
import java.math.RoundingMode
import java.text.DecimalFormat


/**
 * 获取进程号对应的进程名
 *
 * @param pid 进程号
 * @return 进程名
 */
fun getProcessName(pid: Int): String? {
    var reader: BufferedReader? = null
    try {
        reader = BufferedReader(FileReader("/proc/$pid/cmdline"))
        var processName = reader.readLine()
        if (!TextUtils.isEmpty(processName)) {
            processName = processName.trim { it <= ' ' }
        }
        return processName
    } catch (throwable: Throwable) {
        throwable.printStackTrace()
    } finally {
        try {
            reader?.close()
        } catch (exception: IOException) {
            exception.printStackTrace()
        }

    }
    return null
}


//加载圆形网络图片
fun ImageView.setCircleImageUrl(url: Any?) {
    if (url == null) {
        return
    }
    Glide.with(context)
        .load(url)
        .placeholder(R.mipmap.ic_launcher)
        .circleCrop()
        .into(this)
}

/**
 * 加载圆角图片
 * radius 圆角
 * id 默认图
 */
fun ImageView.setRoundImageUrl(url: Any?, radius: Int, id: Int) {
    if (url == null) {
        return
    }
    val roundOptions = RequestOptions()
        .transform(CenterCrop(), RoundedCorners(radius))
    Glide.with(context)
        .load(url)
        .apply(roundOptions)
        .placeholder(id)
        .into(this)
}

/**
 * 加载圆角图片
 * radius 圆角
 */
fun ImageView.setRoundImageUrl(url: Any?, radius: Int) {
    if (url == null) {
        return
    }
    val roundOptions = RequestOptions()
        .transform(CenterCrop(), RoundedCorners(radius))
    Glide.with(context)
        .load(url)
        .apply(roundOptions)
        .placeholder(R.mipmap.ic_launcher)
        .into(this)
}

/**
 * 加载直角图片
 * id 默认头像
 */
fun ImageView.setImageUrl(url: Any?, id: Int) {
    if (url == null) {
        return
    }
    val roundOptions = RequestOptions()
        .transform(CenterCrop())
    Glide.with(context)
        .load(url)
        .apply(roundOptions)
        .placeholder(id)
        .into(this)
}

/**
 * 加载直角图片
 * id 默认头像
 */
fun ImageView.setImageUrl(url: Any?) {
    if (url == null) {
        return
    }
    val roundOptions = RequestOptions()
        .transform(CenterCrop())
    Glide.with(context)
        .load(url)
        .apply(roundOptions)
        .placeholder(R.mipmap.ic_launcher)
        .into(this)
}

/**
 * 吐司
 */
fun String.toast() {
//    ToastUtils.showShort(this)
    com.hjq.toast.ToastUtils.show(this)
}

/**
 * log
 */
fun Any.logI() {
    LogUtils.i(this)
}

fun Any.logD() {
    LogUtils.d(this)
}

fun Any.logW() {
    LogUtils.w(this)
}

fun Any.logE() {
    LogUtils.e(this)
}

fun Any.logV() {
    LogUtils.v(this)
}

/**
 * 保留两位小数
 */
fun Double.format2(): String {
    var df = DecimalFormat("0.00")
    df.roundingMode = RoundingMode.HALF_UP
    return df.format(this)
}

fun String.toPhone(): String {
    return if (this.isNotEmpty()) {
        if (this.length == 11) {
            var phone = this.substring(0, 3) + "****" + this.substring(7, 11)
            phone
        } else {
            this
        }
    } else {
        ""
    }
}

